
const { autoCommit } = require('oracledb')
const db = require('../../lib/conn_oracle')

exports.getLocationInfo = async (req, res) => {
    // console.log(req.body)
    let { persongroup } = req.body
    let pgWhere = persongroup.length ? ` and l.cust_useteam = :persongroup` : ""
    let resMsg = ""
    let resStatus = 1
    let resDatas = {}
    let searchSql = `select *
                     from c##wxw.location l
                     where 1=1
                     ${pgWhere} 
                     order by l.location
                    `
    try {
        let locationInfo = persongroup.length ? await db.search(searchSql, [persongroup]) : await db.search2(searchSql)
        resMsg = locationInfo.errorNum ? "查询失败" : "查询成功"
        resStatus = locationInfo.errorNum ? 0 : 1
        resDatas = { locationInfo }
        res.json({
            resMsg,
            status: resStatus,
            resDatas
        })
    } catch (err) {
        res.json({
            msg: "查询失败" + err,
            status: 0
        })
    }
}

exports.editLocation = async (req, res) => {
    console.log(req.body)
    let { type, data } = req.body
    let { LOCATION, DESCRIPTION, CUST_LINENUM, AREATYPE, REMARK, CUST_USETEAM } = data
    let resMsg = ""
    let msgType = ""
    let resStatus = 1
    let resDatas = {}
    let editDatas = []
    let editSql = ""

    if(type === "add" || type === "copy"){
        msgType = "新增"
        editDatas = [ LOCATION, DESCRIPTION, CUST_LINENUM, AREATYPE, REMARK, CUST_USETEAM ]
        editSql = `insert into c##wxw.location(LOCATION,DESCRIPTION,CUST_LINENUM,AREATYPE,REMARK,CUST_USETEAM)
                   values(:LOCATION,:DESCRIPTION,:CUST_LINENUM,:AREATYPE,:REMARK,:CUST_USETEAM)`
    }
    if(type === "update"){
        msgType = "更新"
        editDatas = [ DESCRIPTION, CUST_LINENUM, AREATYPE, REMARK, CUST_USETEAM,LOCATION ]
        editSql = `update c##wxw.location 
                    set DESCRIPTION=:DESCRIPTION,
                    CUST_LINENUM=:CUST_LINENUM,
                    AREATYPE=:AREATYPE,
                    REMARK=:REMARK,
                    CUST_USETEAM=:CUST_USETEAM 
                    where LOCATION=:LOCATION`
    }
    if(type === "delete"){
        msgType = "删除"
        editDatas = [ LOCATION ]
        editSql = `delete from c##wxw.location where LOCATION=:LOCATION`
    }

    try{
        let newLocation = await db.writeTab(editSql,editDatas)
        resMsg = newLocation.errorNum ? `${msgType}失败` : `${msgType}成功`
        resStatus = newLocation.errorNum ? 0 : 1
        resDatas = { newLocation }
        res.json({
            msg:resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg:`${msgType}失败`+err,
            status:0
        })
    }
}

exports.batchEditLocation = async (req, res) => {
    // console.log(req.body)
    let { type, data } = req.body
    let resMsg = ""
    let msgType = ""
    let resStatus = 1
    let resDatas = {}
    let editDatas = []
    let editSql = ""
    let options = {
        autoCommit: true
    }
    if(type === "batchAdd"){
        msgType = "批量新增"
        editDatas = data
        editSql = `insert into c##wxw.location(LOCATION,DESCRIPTION,CUST_LINENUM,AREATYPE,REMARK,CUST_USETEAM)
                   values(:LOCATION,:DESCRIPTION,:CUST_LINENUM,:AREATYPE,:REMARK,:CUST_USETEAM)`
    }
    if(type === "batchUpdate"){
        msgType = "批量更新"
        editDatas = data
        editSql = `update c##wxw.location 
                    set DESCRIPTION=:DESCRIPTION,
                    CUST_LINENUM=:CUST_LINENUM,
                    AREATYPE=:AREATYPE,
                    REMARK=:REMARK,
                    CUST_USETEAM=:CUST_USETEAM 
                    where LOCATION=:LOCATION`
    }
    if(type === "batchDel"){
        msgType = "批量删除"
        editDatas = data
        editSql = `delete from c##wxw.location where LOCATION=:LOCATION`
    }

    try{
        let newLocation = await db.writeManyTab(editSql,editDatas,options)
        resMsg = newLocation.errorNum ? `${msgType}失败` : `${msgType}成功`
        resStatus = newLocation.errorNum ? 0 : 1
        resDatas = { newLocation }
        res.json({
            msg:resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg:`${msgType}失败`+err,
            status:0
        })
    }

}
